package com.shenmazong.user.controller;

import com.shenmazong.user.entity.TbSysUser;
import com.shenmazong.user.service.TbSysUserService;
import com.shenmazong.utils.FileUploadUtil;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: 平台用户接口
 * @date 2022/6/21 11:03
 */

@RestController
@RequestMapping(value = "/sysUser")
@Slf4j
@Api(tags = "系统用户操作接口")
public class SysUserController {

    @Autowired
    TbSysUserService tbSysUserService;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @ApiOperation("添加管理员")
    @PostMapping(value = "/add")
    public ResultResponse add(@RequestBody TbSysUser tbSysUser) {
        return tbSysUserService.add(tbSysUser);
    }

    @ApiOperation("删除管理员")
    @PostMapping(value = "/del")
    public ResultResponse del(@RequestBody IdVo idVo) {
        return tbSysUserService.del(idVo);
    }

    @ApiOperation("修改管理员")
    @PostMapping(value = "/set")
    public ResultResponse set(@RequestBody TbSysUser tbSysUser) {
        return tbSysUserService.set(tbSysUser);
    }

    @ApiOperation("根据ID查询管理员")
    @PostMapping(value = "/get")
    public ResultResponse get(@RequestBody IdVo idVo) {
        return tbSysUserService.get(idVo);
    }

    /**
     * @description 修改用户密码
     * @author 军哥
     * @date 2022/6/21 15:10
     * @version 1.0
     */
    @ApiOperation("修改管理员密码")
    @PostMapping(value = "/changePassWord")
    public ResultResponse changePassWord(@RequestBody PasswordChangeVo passwordChangeVo) {
        return tbSysUserService.changePassWord(passwordChangeVo);
    }

    @ApiOperation("分页查询管理员")
    @PostMapping(value = "/listByPage")
    public ResultResponse listByPage(@RequestBody PageInfoVo pageInfoVo) {
        return tbSysUserService.listByPage(pageInfoVo);
    }

    @ApiOperation("系统用户登录")
    @PostMapping(value = "/login")
    public ResultResponse login(@RequestBody LoginInfoVo loginInfoVo) {
        return tbSysUserService.login(loginInfoVo);
    }

    @ApiOperation("上传系统用户的头像")
    @PostMapping(value = "/logoUpload")
    public ResultResponse logoUpload(@RequestParam("file") MultipartFile file){
        //--1 使用工具类接收上传图片
        FileUploadUtil fileUploadUtil = FileUploadUtil.upload()
                .setDomain("http://localhost:9040/images/")
                .setUploadDir("E:\\test\\upload");
        String url = fileUploadUtil.saveFile(file);

        //--2 存到数据库里面

        //--3 返回elementui的规则数据
        return ResultResponse.SUCCESS(fileUploadUtil.getFileUrl());
    }

    /**
     * @description 高亮搜索系统用户
     * @author 军哥
     * @date 2022/7/1 16:52
     * @version 1.0
     */

    @PostMapping(value = "/highlight")
    public ResultResponse highlight(@RequestBody SearchVo searchVo) {

        //--1 构造分页
        if(searchVo.getPageNum() > 0) {
            searchVo.setPageNum(searchVo.getPageNum()-1);
        }
        PageRequest pageRequest = PageRequest.of(searchVo.getPageNum(), searchVo.getPageSize());

        //--2 builder
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>").postTags("</font>").field("userSkills");

        //--3 构造查询
        NativeSearchQuery query = builder.withPageable(pageRequest)
                .withQuery(QueryBuilders.matchQuery("userSkills", searchVo.getKeyWord()))
                .withHighlightBuilder(highlightBuilder)
                .build();

        //--4 执行查询
        long count = elasticsearchRestTemplate.count(query, TbSysUser.class);
        SearchHits<TbSysUser> searchHits = elasticsearchRestTemplate.search(query, TbSysUser.class);

        //--5 获取查询的结果
        List<SearchHit<TbSysUser>> hitsSearchHits = searchHits.getSearchHits();
        hitsSearchHits.forEach(item -> {
            TbSysUser tbHero = item.getContent();
            System.out.println(tbHero);
        });

        //--6 手动分页
        ArrayList<TbSysUser> tbHeroes = new ArrayList<>();
        hitsSearchHits.forEach(item -> {
            TbSysUser tbHero = item.getContent();

            List<String> names = item.getHighlightField("userSkills");
            if(names != null && names.size()>0) {
                String s = names.get(0);
                tbHero.setUserSkills(s);
            }

            tbHeroes.add(tbHero);
        });

        HashMap<String, Object> map = new HashMap<>();
        map.put("records", tbHeroes);
        map.put("pageNum", searchVo.getPageNum()+1);
        map.put("pageSize", searchVo.getPageSize());
        map.put("count", count);

        return ResultResponse.SUCCESS(map);
    }
}
